package com.renwei.yunlong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.renwei.yunlong.R;

/**
 * 工单title Adapter
 *
 * Created by Administrator on 2017/9/20.
 */

public class WorkAdapter extends RecyclerView.Adapter<WorkAdapter.WorkOrderViewHolder>{


    private Context context;
    private LayoutInflater inflater;

    String[] title = {"联泰信息系统","华为科技华东分公司","华为科技西南分公司"};

    private int selectedPosition = -1;//记录

    private OnItemClickListener mOnItemClickListener;

    public WorkAdapter(Context context){
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public WorkOrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View  view = inflater.inflate(R.layout.item_title_layout,null);

        return new WorkOrderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final WorkOrderViewHolder holder, int position) {
        holder.tvTitle.setText(title[position]);
        if (selectedPosition != -1){
            if (selectedPosition == position){
                //选中
                holder.ivChosen.setVisibility(View.VISIBLE);
            }else {
                holder.ivChosen.setVisibility(View.GONE);
            }
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                int pos = holder.getLayoutPosition();
                mOnItemClickListener.onItemClick(holder.itemView,pos,title[pos]);
            }
        });
    }

    @Override
    public int getItemCount() {
        return title.length;
    }

    public void setSelection(int position) {
        selectedPosition = position;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){

        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int position,String title);
    }

    static class WorkOrderViewHolder extends RecyclerView.ViewHolder{

        private View itemView;
        private TextView tvTitle;
        private ImageView ivChosen;

        public WorkOrderViewHolder(View view) {
            super(view);
            itemView = view;
            tvTitle = (TextView) itemView.findViewById(R.id.text_title);
            ivChosen = (ImageView) itemView.findViewById(R.id.iv_chosen);
        }
    }
}
